<template>
  <div class="container">
    <div class="search-box">
      <el-input
        placeholder="请输入内容"
        class="input-box"
        v-model="input"
      ></el-input>
      <el-button>清除</el-button>
      <el-button type="primary" @click="sousuoA">搜索</el-button>
    </div>

    <el-table border style="width: 100%" :data="list">
      <el-table-column type="index" label="任务编号" width="80">
      </el-table-column>
      <el-table-column prop="name" label="任务名称"> </el-table-column>
      <el-table-column prop="des" label="任务描述"> </el-table-column>
      <el-table-column fixed="right" label="操作" width="180">
        <template #default="{ row }">
          <el-button
            size="mini"
            style="margin-right: 10px"
            @click="compile(row.id)"
            >编辑</el-button
          >
          <el-popconfirm
            confirmButtonText="好的"
            cancelButtonText="不用了"
            icon="el-icon-info"
            iconColor="red"
            title="确定删除任务吗？"
            @confirm="Popconfirmconfirm(row.id)"
            @cancel="Popconfirmcancel"
          >
            <template #reference>
              <el-button size="mini" type="danger">删除</el-button>
            </template>
          </el-popconfirm>
        </template>
      </el-table-column>
    </el-table>

    <div class="pageContainer">
      <!-- 分页 -->
      <el-pagination
        layout="prev, pager, next"
        :total="total"
        :page-size="paging._limit"
        @current-change="sizeChange"
      >
      </el-pagination>

      <el-button size="small" @click="add" plain>+ 新增</el-button>
    </div>

    <!-- 新增框 -->
    <el-dialog title="提示" :visible.sync="dialogVisible" width="30%">
      <el-form ref="form" :model="ruleForm" :rules="rules" label-width="80px">
        <el-form-item label="任务名称" prop="name">
          <el-input v-model="ruleForm.name"></el-input>
        </el-form-item>
        <el-form-item label="任务描述" prop="des">
          <el-input v-model="ruleForm.des"></el-input>
        </el-form-item>
      </el-form>
      <span slot="footer" class="dialog-footer">
        <el-button @click="cancel">取 消</el-button>
        <el-button type="primary" @click="confirm">确 定</el-button>
      </span>
    </el-dialog>
  </div>
</template>

<script>
import {
  InterfaceList,
  Added,
  deletes,
  compiles,
  particulars,
  sousuo,
} from "./api";
import { v4 as uuidv4 } from "uuid";
export default {
  data() {
    return {
      list: [],
      paging: {
        _page: 1,
        _limit: 5,
      },
      total: 0,

      input: "",

      ruleForm: {
        name: "",
        des: "",
        id: uuidv4(),
      },
      id: null,
      rules: {
        name: [
          { required: true, message: "请输入任务名称", trigger: "blur" },
          { min: 3, max: 5, message: "长度在 3 到 5 个字符", trigger: "blur" },
        ],
        des: [
          { required: true, message: "请输入任务描述", trigger: "blur" },
          { min: 3, max: 9, message: "长度在 3 到 9 个字符", trigger: "blur" },
        ],
      },
      dialogVisible: false,
    };
  },

  mounted() {
    this.getlist();
  },

  methods: {
    // 获取数据
    async getlist() {
      const res = await InterfaceList(this.paging);
      // console.log(res);
      this.list = res.data;
      this.total = Number(res.headers["x-total-count"]);
    },

    // 分页器
    sizeChange(_page) {
      this.paging._page = _page;
      console.log(_page);
      this.getlist();
    },

    // 打开新增弹框
    add() {
      this.dialogVisible = true;
    },

    // 重置弹框
    chongzhi() {
      this.ruleForm = {
        name: "",
        des: "",
      };
    },

    // 取消新增
    cancel() {
      this.dialogVisible = false;
      this.chongzhi();
      this.$refs.form.resetFields();
    },

    // 确定新增
    confirm() {
      this.$refs.form.validate(async (valid) => {
        if (valid) {
          if (this.id) {
            await compiles({
              name: this.ruleForm.name,
              des: this.ruleForm.des,
              id: this.id,
            });
            this.$message.success("修改成功");
          } else {
            await Added(this.ruleForm);
            this.$message.success("添加成功");
          }
          this.dialogVisible = false;
          this.$refs.form.resetFields();
          this.getlist();
          this.chongzhi();
        }
      });
    },

    // 确认删除
    async Popconfirmconfirm(id) {
      // console.log(id);
      await deletes(id);
      this.$message.success("删除成功");
      this.getlist();
    },

    // 取消删除
    Popconfirmcancel() {
      // console.log(123456);
    },

    // 编辑
    async compile(id) {
      this.dialogVisible = true;
      const res = await particulars(id);
      // console.log(res);
      this.ruleForm = res.data;
      this.id = id;
    },

    // 搜索
    async sousuoA() {
      const res = await sousuo(this.input);
      // console.log(res);
      // this.paging = {
      //   _page: 1,
      //   _limit: 1,
      // };
      // this.total = 1;
      this.list = res.data;
    },
  },
};
</script>

<style scoped lang="less">
.container {
  width: 800px;
  margin: 0 auto;
}
.search-box {
  display: flex;
  margin-bottom: 10px;
  .input-box {
    margin-right: 20px;
  }
}
.pageContainer {
  margin-top: 10px;
  display: flex;
  justify-content: space-between;
}
</style>
